package com.sgzn.sgpay.service.tcp.receive.handler;

import com.sgzn.sgpay.service.tcp.receive.TcpReceiveCallHandlerTemplate;
import com.sgzn.sgpay.service.tcp.receive.modle.TcpReceiveCallRequest;
import com.sgzn.sgpay.service.tcp.receive.modle.response.TcpQueryTerminalRecordBabyResult;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 查询终端账目应答（娃娃机） 处理器
 * @author huangsenguang
 * @version Id: TcpQueryTerminalRecordBabyHandler.java, v 0.1 2018/7/29 15:42 huangsenguang Exp $$
 */
@Service
@Slf4j
public class TcpQueryTerminalRecordBabyHandler extends TcpReceiveCallHandlerTemplate<TcpQueryTerminalRecordBabyResult> {
    /**
     * 协议总字符数 娃娃机
     */
    @Getter
    private final int protocolLength = 46;

    @Override
    public TcpQueryTerminalRecordBabyResult callInner(TcpReceiveCallRequest request) {
        String data = request.getData();
        TcpQueryTerminalRecordBabyResult result = new TcpQueryTerminalRecordBabyResult();
        result.setDeviceState(data.substring(0,2));
        result.setBabyLocalIncomeTotal(parse(data,2,10));
        result.setRefundIncrement(parse(data,8,18));
        result.setExchangeIncomeTotal(parse(data,16,26));
        result.setOnlineCoinAmount(parse(data,26,34));
        return result;
    }

    @Override
    protected int getProtocolLength(TcpReceiveCallRequest request) {
        return this.protocolLength;
    }

    private int parse(String data, int beginIndex, int endIndex) {
        return Integer.parseInt(SgTcpUtil.sortTcp(data.substring(beginIndex, endIndex)), 16);
    }
}

